package com.yc.supermarket.web.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.yc.supermarket.common.AlipayTradePagePay;
import com.yc.supermarket.common.AlipayTradePrecreate;
import com.yc.supermarket.common.Order;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/pay")
public class payController extends BaseController {

    //电脑端网页支付
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String total = req.getParameter("total_amount");
        Order Content = new Order();
        Content.setTotal_amount(total);

        System.out.print(total+"======================---------------");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        try {
            /** 获取接口调用结果 **/
            AlipayTradePagePayResponse payResponse = AlipayTradePagePay.pay(Content);
            // payResponse.getOutTradeNo();//商户的自定义订单号
            //payResponse.getTradeNo()//支付宝交易号
            String str=payResponse.getBody();
            System.out.println(str);
            resp.setStatus(200);

            resp.getWriter().write(str);

//            resp.sendRedirect( payResponse.getBody());
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String total = req.getParameter("total_amount");
        Order Content = new Order();
        Content.setTotal_amount(total);
        try {
            /** 获取接口调用结果 **/
            AlipayTradePrecreateResponse payResponse = AlipayTradePrecreate.pay(Content);
            // payResponse.getOutTradeNo();//商户的自定义订单号
            System.out.println(payResponse.getBody());
            System.out.println("payResponse.getQrCode() = " + payResponse.getQrCode());
            HttpSession session = req.getSession();
            session.setAttribute("qccode", payResponse.getQrCode());
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
//        resp.setStatus(200);
    }
}
